<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topbarwarp">
      <!-- 左侧标题 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo1.headImg" width="24px" height="24px" alt="" />
        </li>

        <li>用户名：{{ userInfo1.nickName }}</li>
        <li>我的积分:{{ userInfo1.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="login-btn" v-if="!isLogined" @click="changeIsShwoLoginMoadl(true)">登录</li>
        <li class="cart-btn" v-else>
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
      </ul>
    </div>
  </div>
</template>
 
<script>
import { mapActions, mapMutations, mapState } from 'vuex'
import { WeixinLoginApi, userInfo } from '@/request/api'
export default {
  data() {
    return {}
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal: (state) => state.userinfo.cartTotal,
      userInfo1: (state) => state.userinfo.userInfo1
    })
  },
  methods: {
    ...mapMutations({
      changeIsShwoLoginMoadl: 'showModal/changeIsShwoLoginMoadl',
      changeLoginMoadl: 'loginStatus/changeLoginMoadl',
      updataUserInfo: 'userinfo/updataUserInfo',
      initUserInfo: 'userinfo/initUserInfo'
    }),
    ...mapActions({
      asyncChangeToast: 'showToast/asyncChangeToast',
      asyncChangeUserInfo: 'userinfo/asyncChangeUserInfo'
    })
  },
  // watch: {
  //   //监听路由地扯的变化
  //   '$route.path': {
  //     handler(newVal, oldVal) {
  //       if (newVal != oldVal) {
  //         //判断当前是否还有token值
  //         let token = localStorage.getItem('x-auth-token')
  //         /* if (token) {
  //           this.changeLoginMoadl(true)
  //         } else {
  //           this.changeLoginMoadl(false)
  //         } */
  //         this.changeLoginMoadl(Boolean(token))
  //       }
  //     }
  //   }
  // },
  //项目打开，页面刷新
  //微信挚友后，页面自动刷新
  async created() {
    // console.log(this.$route.query.code)
    let code = this.$route.query.code
    if (code) {
      let res = await WeixinLoginApi({
        code
      })
      // console.log('微信登录后res:', res)
      if (res.code == 0) {
        //提示
        this.asyncChangeToast({
          isshow: true,
          msg: '请滑动滑动验证码',
          type: 'danger'
        })
        //重新存token 因为刷新就重新加载了
        localStorage.setItem('x-auth-token', res['x-auth-token'])
        //登录就要将购物车显示出来
        this.changeLoginMoadl(true)
        //清除过扯栏中的code
        this.$route.query.code && this.$router.push(this.$route.path)
        this.asyncChangeUserInfo()
      } else if (res.code == 400) {
        /* 提示重新扫码 */

        this.asyncChangeToast({
          isshow: true,
          msg: '二维码打开失败',
          type: 'danger'
        })
        /* 自动打开登录模块 */
        this.changeIsShwoLoginMoadl(true)
      } else if (res.code == 407) {
        this.asyncChangeToast({
          isshow: true,
          msg: '请先用手机号码登录 ',
          type: 'danger'
        })
        this.changeIsShwoLoginMoadl(true)
        localStorage.setItem('uuid', res.uuid)
        // console.log(res.uuid)
      }
    } else {
      let token = localStorage.getItem('x-auth-token')
      this.changeLoginMoadl(Boolean(token))
      if (token) {
        this.asyncChangeUserInfo()
        /* 用户信息初始始 */
      } else {
        this.initUserInfo()
      }
    }
  }
}
</script>
 <!-- scoped  只在当前组件内有效 -->
<style lang = "less" scoped>
.topbar {
  width: 1920px;
  height: 40px;
  background: #242b39;
  font-size: 14px;
  line-height: 40px;
  color: #fffefe;
  margin: 0 auto;
  .topbarwarp {
    display: flex;
    justify-content: space-between;
    text-align: center;
    .right {
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
      li {
        cursor: pointer;
        float: left;
        margin-left: 20px;
        padding: 0px 10px;
        &.login-btn {
          width: 124px;
          background-color: #0a328e;
        }
        img {
          vertical-align: middle;
          border-radius: 50%;
        }
      }
      li:hover {
        background: tomato;
      }
    }
  }
}
</style>